<!doctype html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>型号管理</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/webapp/js/common/metadata.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
</head>
<body>
<div class="easyui-container-fluid">
    <div class="easyui-panel" style="border: none;">
        <div class="nav" id="nav"></div>
        <div class="easyui-panel" style="border:none;">
            <form id="ff">
                <div style="margin-bottom:20px;">
                    <label class="item-label">品类：</label>
                    <select id="categorySelect" name="categoryCode" class="easyui-combobox" style="width:100px;">
                    </select>
                    <label class="item-label">品牌：</label>
                    <select id="brandSelect" name="brandCode" class="easyui-combobox" style="width:100px;">
                    </select>
                    <label id="seriesTo3cDiv">
                        <label class="item-label">系列：</label>
                        <select class="easyui-combobox" id="seriesTo3cCode" name="seriesTo3cCode" style="width:110px;display: none" ></select>
                    </label>
                    <label class="item-label">卖点：</label>
                    <!--//借用系列查卖点-->
                    <!--<input type="text" name="code"  class="easyui-textbox" style="width:100px;"/>-->
                    <input type="text" class="easyui-textbox" name="series" style="width:100px">

                    <label class="item-label">状态：</label>
                    <input id="entityStatus" class="easyui-switchbutton" checked style="width:80px;height:30px;" onText="启用" offText="禁用"  >
                    <div style="display:inline-block;margin-left: 40px;" >
                        <a href="javascript:void(0);" class="easyui-linkbutton c8" onclick="cq()" style="width:80px;margin-left: 40px;">复杂条件</a>
                        <a href="javascript:void(0);" class="easyui-linkbutton c8" id="ycxg" onclick="bUpdate()" style="width:80px;margin-left: 40px;">批量修改</a>
                    </div>
                </div>
                <div style="margin-bottom:20px;">
                    <!--<a class='l-btn-left l-btn-icon-left' style='width:24px;height:24px;'><span class='l-btn-icon icon-search'></span></a>-->
                    <label class="item-label">代码：</label>
                    <!--<input type="text" name="code"  class="easyui-textbox" style="width:100px;"/>-->
                    <input class="easyui-searchbox" name="code" data-options="searcher:searchCategory" style="width:100px">
                    <label class="item-label">型号：</label>
                    <input type="text" name="likeName" id="likeName"  class="easyui-textbox" style="width:100px;"/>
                    <span style="margin-left: 10px">
                         <label class="item-label">操作人：</label>
                         <input type="text" class="easyui-combobox" name="operateUser" id="user" style="width:100px;"/>
                    </span>
                    <label class="item-label">规格状态：</label>
                    <select name="propStatusCt" class="easyui-combobox" data-options="panelHeight:'auto'" style="width:60px;">
                        <option value="EQUAL">=</option>
                        <option value="NOT_EQUAL" ><></option>
                    </select>
                    <select name="propStatus" class="easyui-combobox" data-options="panelHeight:'auto'" style="width:100px;">
                        <option value="">--请选择--</option>
                        <option value="CONFIRMALL">全部确认</option>
                        <option value="CONFIRMONE" >部分确认</option>
                        <!--<option value="CONFIRMTWO" >两级</option>-->
                        <!--<option value="CONFIRMTHREE">三级</option>-->
                        <option value="UNCOFIRMED">未确认</option>
                        <option value="UNABLECOFIRED">无法找到信息</option>
                        <option value="UNRELEASED">暂未发布</option>
                    </select>


                    <a href="javascript:void(0);" class="easyui-linkbutton c8" onclick="searchBy()" style="width:80px;margin-left: 40px;">查询</a>
                </div>
            </form>
        </div>
        <div class="table-wrapper">
            <table id="datagrid" data-options="fitColumns:true,scrollBarSize:0">
                <thead>
                <tr>
                    <th data-options="field:'code',align:'center'" width="80">代码</th>
                    <th data-options="field:'name',align:'center'" width="150">颜色名</th>
                    <th data-options="field:'parentName',align:'center'" width="80">型号</th>
                    <th data-options="field:'otherNames',align:'center'" width="200">别名</th>
                    <th data-options="field:'brandName',align:'center'" width="80">品牌</th>
<!--                    <th data-options="field:'categoryName',align:'center'" width="80">品类</th>-->
                    <th data-options="field:'onSellYear',align:'center'" width="60">上市年份</th>
                    <th data-options="field:'onSellMonth',align:'center'" width="60">上市月</th>
                    <th data-options="field:'onSellWeek',align:'center'" width="60">上市周</th>
                    <th data-options="field:'onSellPrice',align:'center'" width="60">上市价格</th>
                    <th data-options="field:'codeValue',align:'center',formatter:codeValue" width="100">规格值</th>
                    <th data-options="field:'updateTime',align:'center'" width="150">修改时间</th>
                    <th data-options="field:'disableTime',align:'center'" width="150">禁用时间</th>
                    <th data-options="field:'entityStatus',align:'center',formatter:statusFormatter" width="40">状态</th>
                    <th data-options="field:'operateUser',align:'center'" width="60">操作人</th>
                    <th data-options="field:'action',align:'center',formatter:actions" width="180">操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div id="dd" class="easyui-window upload-wrapper" title="禁用原因" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:330px;height:220px;padding:10px;">
    <div class='box'><label class='item-label'>
        <input type="text" value="" id="id" style="display:none">
        <input type="text" value="" id="modelCode" style="display:none">
        <input type="text" value="" id="brandCode" style="display:none">
        <input type="text" value="" id="categoryCode" style="display:none">
        <span  class='item-required' style="font-size:18px" >*禁用理由：</span></label><br>&nbsp &nbsp
        <label style='padding:0 10px;' style="font-size:18px"><input type='checkbox' name='data'class ="checkAll" value='I' style='margin-right:10px; '/>型号重复</label> &nbsp
        <input type="text" name="modelCode" id="mc" class="easyui-textbox" placeholder="请输入型号码"style="width:100px;"/><br/>&nbsp &nbsp

        <label style='padding:0 10px;' style="font-size:18px"><input type='checkbox' name='data' class ="checkAll" value='II' style='margin-right:10px; '/>品牌错误</label>&nbsp
        <input type="text" name="correctBrandCode" id="bc" class="easyui-textbox" placeholder="请输入品牌码"style="width:100px;"/><br/>&nbsp &nbsp
        <label style='padding:0 10px;' style="font-size:18px"><input type='checkbox' name='data' class ="checkAll" value='III' style='margin-right:10px; '/>品类错误</label>&nbsp
        <input type="text" name="correctCategoryCode" id="cc" class="easyui-textbox" placeholder="请输入品类码"style="width:100px;"/><br/>&nbsp &nbsp
        <label style='padding:0 10px;' style="font-size:18px"><input type='checkbox' name='data'class ="checkAll"  value='VI' style='margin-right:10px; '/>其他</label>
    </div>

    <div style="margin-bottom:2px;text-align:center">
        <a style="width:60px;height: 30px;" href="javascript:void(0);" class="easyui-linkbutton c8" onclick="clickHandler()">保存</a>
    </div>
</div>
<div id="cq" class="easyui-window upload-wrapper " title="高级查询构造器" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:745px;height:500px;padding:10px;">
<form id="sql">
    <div id="wrap-grup">
        <div class="bkCss" id="wrap-grup1" style="border: 1px solid #5ffffd;">
            <div>
                <input id="andOr1" name ="andOr1" class="easyui-switchbutton " checked style="width:80px;height:30px; padding: 20px" onText="and" offText="or"  >
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" style="width:120px;margin-left: 250px" onclick="addColumn('wrap-ul1')">添加新列</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" style="width:120px" onclick="addGroup()">添加新组</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" style="width:120px" onclick="reGroup('wrap-grup1')">remove</a>
            </div>
            <div  id="wrap-ul1">
                <div id="wrap-column">
                    <input type="text" name="propCode1" id="propCode1" class="easyui-combobox" style="width:200px; margin-left: 50px" data-options="panelHeight:'auto'" />
                    <input type="text" name="comparator1" id="comparator1" class="easyui-combobox" style="width: 180px;margin-left: 100px" data-options="panelHeight:'auto'" prompt="比较符"/>
                    <div style="display: inline-block;margin-left: 50px" id="metadatavalue1">
                    </div>
                    <a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;'data-options="iconCls:'icon-cancel'" onclick='rmColumn("wrap-column")'></a>&nbsp;&nbsp;
                </div>
            </div>

        </div>
    </div>
    <div style="margin-bottom:2px;text-align:center">
        <a style="width:60px;height: 30px;" href="javascript:void(0);" class="easyui-linkbutton c8" onclick="spliceSql()">确定</a>
    </div>
</form>
</div>
<!--更新字段 只针对复杂条件-->
<div id="update" class="easyui-window upload-wrapper " title="批量更新部分字段" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:745px;height:500px;padding:10px;">
    <form id="batchUpdate">
        <div>
            <span>根据复杂条件，批量修改</span>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" style="width:120px;margin-left: 250px" onclick="updateColumn('update-ul')">添加新列</a>
        </div>
        <div  id="update-ul">
            <div id="update-column">
                <input type="text" name="propCodeU" id="propCodeUpdate" class="easyui-combobox" style="width:200px; margin-left: 50px" data-options="panelHeight:'auto'" />
                <div style="display: inline-block;margin-left: 50px" id="updateValue">
                </div>
                <a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;'data-options="iconCls:'icon-cancel'" onclick='rmColumn("update-column")'></a>&nbsp;&nbsp;
            </div>
        </div>
        <div style="margin-bottom:2px;text-align:center">
            <a style="width:100px;height: 30px;" href="javascript:void(0);" class="easyui-linkbutton c8" onclick="batchUpdate()">确认修改</a>
        </div>
    </form>
</div>
</body>
<script type="text/javascript">
    var criterias="";
    var groupId=1;
    var ulId=1;
    var upId=1;
    var categoryType;
    var categoryCode;
        $('#mc').textbox({
            prompt: '请输入型号码'
        });
        $('#bc').textbox({
            prompt: '请输入品牌码'
        });
        $('#cc').textbox({
            prompt: '请输入品类码'
        });
    function initPage() {
        $("#seriesTo3cDiv").hide();
        $("#comparator").combobox({
            data:[
                {text:"=",value:"EQUAL"},
                {text:">",value:"GREAT"},
                {text:">=",value:"GREAT_EQUAL"},
                {text:"<",value:"LITTLE"},
                {text:"<=",value:"LITTLE_EQUAL"},
                {text:"<>",value:"NOT_EQUAL"},
                {text:"包含",value:"CONTAIN"}]
        });
        $("#comparator1").combobox({
            data:[
                {text:"=",value:"EQUAL"},
                {text:">",value:"GREAT"},
                {text:">=",value:"GREAT_EQUAL"},
                {text:"<",value:"LITTLE"},
                {text:"<=",value:"LITTLE_EQUAL"},
                {text:"<>",value:"NOT_EQUAL"},
                {text:"包含",value:"CONTAIN"}]
        });
        var v=window.parent.userName
        if("admin" != v && "lyp" != v){
            $('#ycxg').hide();
        }

        initUserSelect("user");
        initUserCategorySelect("categorySelect",function (code) {
            categoryCode = $("#categorySelect").combobox("getValue");
            getCategoryType();
            initModelSearchSelect("propCode","metadatavalue",code);
            initModelSearchSelect1("propCode1","metadatavalue1",code);
            initModelSearchSelect1("propCodeUpdate","updateValue",code,"updateValue");
            initBrandSelectC("brandSelect",function () {initSeries(categoryCode,"brandSelect")},null,code);
        });
        searchBy(1,100);
    }
    $(function(){
        // initBrandSelect("brandSelect",function () {initSeries()},);
        var height = $(window).height();
        var nav_height = $("#nav")._outerHeight();
        var ff_height = $("#ff").height();
        var table_height = height-nav_height-ff_height-40;
        $("#datagrid").css("max-height",table_height);
        $("#datagrid").datagrid({
            rownumbers:true,
            pagination:true,
            idField:"id",
            checkOnSelect:false,
            toolbar:[{
                text:'添加型号',
                handler:function(){
                    var category = getFormData(document.getElementById("ff")).categoryCode;
                    if(category){
                        window.parent.addTab("添加型号","/webapp/pages/product/model/add.html?categoryCode="+category);
                    }else{
                        $.messager.alert("提示","请选择品类","warn");
                    }
                }
            }]
        }).datagrid("getPager").pagination({
            onSelectPage:searchBy
        });
        initPage();
        aRecords("型号管理","/webapp/pages/product/model")
    });
    function brandFormatter(val, row){
        return val+''+row.brandCode;
    }
    function categoryFormatter(val, row){
        return val+''+row.categoryCode;
    }

    function codeValue(val,row){
        if (!row.properties) {
            return "无规格";
        }
        var props = row.properties;
        var data = getFormData(document.getElementById("sql"));
        var ggxs=[];//规格显示
        var xsgg="";
        if(data.propCode1){
            if(data.propCode1.constructor==Array){
                data.propCode1.forEach(function(code){
                ggxs.push(code);
                })
            }else{
                ggxs.push(data.propCode1);
            }
        }
        for (var j=0;j<ggxs.length;j++){
            var cz=false;
            for(var i=0;i<props.length;i++) {
                if(props[i].code == ggxs[j]){
                    cz=true;
                    xsgg=xsgg+props[i].value+",";
                }
            }
            if(!cz){
                xsgg=xsgg+"null,";
            }
        }
        return xsgg;
    }

    function searchBy(page,size) {
        var dtd = $.Deferred();
        var data = getFormData(document.getElementById("ff"));
        data.reportType=window.parent.workMode.toUpperCase();
        data.likeName=$("#likeName").textbox('getValue');
        var pager = {page:page,size:size};
        if(!size){
            pager['page']= 1;
            pager['size'] = $("#datagrid").datagrid("getPager").pagination("options").pageSize||100;
        }
        var jsonobj = {};
        if(data.propCode&&data.propValue&&data.comparator){
            data.properties = [{code:data.propCode,comparator:data.comparator,value:data.propValue}];
        }else{
            setPropGroup(data);
        }
        // jsonobj.complexProperties=ComplexProperties;
        jsonobj.entityStatus = $("#entityStatus").switchbutton("options").checked==true?"OK":"DISABLE";
        jsonobj = $.extend(true,jsonobj,transfer(data));
        jsonobj = JSON.stringify(jsonobj);
        $.ajax({
            url:config.url + "/model/page?"+transfer2url(pager),
            type:'post',
            data:jsonobj,
            contentType:"application/json;charset=UTF-8",
            dataType:'json',
            xhrFields:{withCredentials:true},
            beforeSend:function () {
                $.messager.progress({
                    title:"请稍等",
                    interval:50,
                    text:"数据加载中..."
                });
            },
            complete:function () {
                $.messager.progress("close");
            },
            success:function (data) {
                $.messager.progress("close");
                if(data){
                    globalData = data;
                    $("#datagrid").datagrid("loadData",data);
                    dtd.resolve();
                }
            },
            error:function (XMLHttpRequest,textStatus,errorThrown) {

            }
        });
        return dtd.promise();
    }

    function setPropGroup(prom) {
        var data = getFormData(document.getElementById("sql"));
        var subscript=0;//存下标
        var ComplexQueryVOs = [];

        if( !data || !data.propValue1 ){
            return;
        }
        if((typeof data.propValue1=='object')&& data.propValue1.constructor==Array){
            for(var a=1;a<=groupId;a++){
                var andOr="andOr"+a;
                var zss=$("#wrap-ul"+a).children().length//每个组的条件
                if(zss==0){
                    return;
                }
                var ComplexQueryVO={};
                if(  data[andOr] == undefined){
                    ComplexQueryVO.andOr ="OR";
                }else {
                    ComplexQueryVO.andOr ="AND";
                }
                var pcpVOs = [];
                for (var i=0;i<zss;i++){
                    var y=i+subscript;
                    var propCode=data.propCode1[y];
                    var comparator=data.comparator1[y];

                    var value=data.propValue1[y];
                    if(propCode&&comparator&&value){
                        var prop ={};
                        prop.code = propCode;
                        prop.value = value;
                        var PcpVO = {};
                        PcpVO.prop = prop;
                        PcpVO.comparator = comparator;
                        pcpVOs.push(PcpVO);
                    }
                }
                subscript=subscript+zss;
                ComplexQueryVO.pcpVO = pcpVOs;
                ComplexQueryVOs.push(ComplexQueryVO);
            }
        }else{
            prom.properties = [{code:data.propCode1,comparator:data.comparator1,value:data.propValue1}];
            prom.comparator=data.comparator1;
        }

        prom.complexProperties = ComplexQueryVOs;
    }
</script>
<!--
<script type="text/javascript" src="/webapp/js/common/pagelist.js"></script>
-->
<script type="text/javascript">
    function actions(val, row){
        var disableBtnText = row.entityStatus=='OK'?'禁用':"启用";
        return "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' "+
            "style='width:60px;' onclick='edit(\""+row.id+"\",\""+row.categoryCode+"\")'>修改</a>&nbsp;&nbsp;"+
            "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;' onclick='updateStatus(\""+row.id+"\", \"" + row.entityStatus +"\", \""
            + row.code +"\", \"" + row.brandCode +"\", \"" + row.categoryCode + "\")'>"+disableBtnText+"</a>&nbsp;&nbsp;"+
            "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;' onclick='skuList(\""+ row.code + "\")'>sku管理</a>";
    }
    function edit(id,categoryCode){
        window.parent.addTab("修改型号", "/webapp/pages/product/model/add.html?id="+id+"&categoryCode="+categoryCode);
    }
    function updateStatus(id,entityStatus,modelCode,brandCode,categoryCode){
        // alert(entityStatus)
        if(entityStatus=="OK"){
            // id=id;
            $("#id").val(id);
            $("#modelCode").val(modelCode);
            $("#brandCode").val(brandCode);
            $("#categoryCode").val(categoryCode);
            $("#mc").textbox('setValue','')
            $("#bc").textbox('setValue','')
            $("#cc").textbox('setValue','')
            $(".checkAll").prop("checked",false);//重新请求数据的时候把全选框变为未选中
            $("#dd").window("open");
        }else{
            $.messager.confirm("提示","您确定禁用/启用吗？",function (f) {
                if(f){
                    $.ajax({
                        url:config.url+"/model/"+id,
                        type:"DELETE",
                        dataType:"json",
                        data:JSON.stringify(id),
                        contentType:"application/json;charset=UTF-8",
                        xhrFields:{withCredentials:true},
                        beforeSend:function () {
                            $.messager.progress({
                                title:"请稍等",
                                interval:50,
                                text:"操作进行中..."
                            });
                        },
                        complete:function () {
                            $.messager.progress("close");
                            $.messager.alert("提示","操作成功","info",function () {
                                searchBy(1,100);
                            });
                        },
                        success:function (data) {
                            $.messager.alert("提示","操作成功","info",function () {
                                searchBy(1,100);
                            });
                        },
                        error:function (data) {

                        }
                    });
                }
            });
        }

    }
    function skuList(modelCode){
        window.parent.addTab("型号sku管理", "/webapp/pages/product/model/modelSku.html?modelCode="+modelCode);
    }

    function clickHandler() {

        var jsonobj = {};
        var disableDetails=[];
        var mc = $("#mc").textbox('getValue')
        var bc = $("#bc").textbox('getValue')
        var cc = $("#cc").textbox('getValue')
        var users = document.getElementsByName("data"); //得到所有的checkbox
            for(var i=0; i<users.length; i++){
                if(users[i].checked){ //如果checkbox被选中
                    console.log($('#modelCode').val());
                    if(users[i].value=="I" && mc=="" ||$('#modelCode').val()==mc){
                        $.messager.alert("提示","请输入重复型号码","error");
                        return;
                    }
                    if(users[i].value=="II" && bc=="" || $('#brandCode').val()==bc){
                        $.messager.alert("提示","请输入正确品牌码","error");
                        return;
                    }
                    if(users[i].value=="I" && mc=="" || $('#categoryCode').val()==cc){
                        $.messager.alert("提示","请输入正确品类码","error");
                        return;
                    }
                    disableDetails.push(users[i].value); //将被选中checkbox背后的值添加到数组中
                }
            }
            jsonobj.id=$('#id').val();
            jsonobj.entityStatus = $("#entityStatus").switchbutton("options").checked==true?"OK":"DISABLE";
            jsonobj.disableDetails=disableDetails;
            jsonobj.modelCode=mc;
            jsonobj.correctCategoryCode=cc;
            jsonobj.correctBrandCode=bc;
            jsonobj = JSON.stringify(jsonobj);
            $("#dd").window("close");
            $.ajax({
                url:config.url+"/model/disable",
                dataType:"json",
                type:'post',
                data:jsonobj,
                contentType:"application/json;charset=UTF-8",
                xhrFields:{withCredentials:true},
                beforeSend:function () {
                    $.messager.progress({
                        title:"请稍等",
                        interval:50,
                        text:"操作进行中..."
                    });
                },
                success:function (data) {
                    if (data.code == "200") {
                        $.messager.progress("close");
                        $("#dd").window("close");
                        $.messager.alert("提示","操作成功","info",function () {
                            searchBy(1,100);
                        });
                    }else{
                        $.messager.progress("close");
                        $.messager.alert("提示",data.msg,"error");
                    }

                },
                error:function (data) {

                }
            });
    }
    $("#ff").keydown(function (event) {
        var e = event || window.event;
        var keyCode = e.keyCode || e.which;
        if(keyCode=="13"){
            searchBy();
        }
    });

</script>
<script>
    function getCategoryType() {
        $.ajax({
            url: config.url + "/category/get",
            type: "get",
            data: {code:categoryCode},
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.code == 200) {
                    categoryType = data.msg.type;
                    if (categoryType == 2) {
                        $("#seriesTo3cDiv").show();
                    }else {
                        $("#seriesTo3cDiv").hide();
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }

    function cq() {
        $("#cq").window("open");
    }
    function bUpdate() {
        var v=window.parent.userName
        if("admin" == v || "lyp" == v){
            $("#update").window("open");
        }else{
        alert("抱歉，本按钮您没有权限")
        }

    }
    function addColumn(id) {
        ulId=ulId+1;
        var inner = "<div id=\"wrap-column"+ulId+"\">\n"+
            "            <input type=\"text\" name='propCode1'id='propCode"+ulId+"' class=\"easyui-combobox\" style=\"width:200px; margin-left: 50px\" data-options=\"panelHeight:'auto'\" />\n" +
            "            <input type=\"text\" name='comparator1' id='comparator"+ulId+"' class=\"easyui-combobox comparator\" style=\"width: 180px;margin-left: 100px\" data-options=\"panelHeight:'auto'\" prompt=\"比较符\"/>\n" +
            "            <div style=\"display: inline-block;margin-left: 50px\" id='metadatavalue"+ulId+"' >\n" +
            "            </div><a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;'data-options=\"iconCls:'icon-cancel'\" onclick=\"rmColumn('wrap-column"+ulId+"')\"></a>&nbsp;&nbsp;\n" +
            "            </div>" +
            "        </div>\n"

        $(inner).appendTo("#"+id);
        $.parser.parse($("#propCode"+ulId).parent());
        var categoryCode = getFormData(document.getElementById("ff")).categoryCode;
        initModelSearchSelect1("propCode"+ulId+"","metadatavalue"+ulId+"",categoryCode);
        $("#comparator"+ulId).combobox({
            data:[
                {text:"=",value:"EQUAL"},
                {text:">",value:"GREAT"},
                {text:">=",value:"GREAT_EQUAL"},
                {text:"<",value:"LITTLE"},
                {text:"<=",value:"LITTLE_EQUAL"},
                {text:"<>",value:"NOT_EQUAL"},
                {text:"包含",value:"CONTAIN"}]
        });
    }
    function addGroup() {
        groupId=groupId+1;
        ulId=ulId+1;
        var inner = "<div class=\"bkCss\" id=\"wrap-grup"+groupId+"\" style=\"border: 1px solid #5ffffd;\">\n" +
            "        <div>\n" +
            "            <input id='andOr"+groupId+"' name='andOr"+groupId+"' class=\"easyui-switchbutton \" checked style=\"width:80px;height:30px; padding: 20px\" onText=\"and\" offText=\"or\"  >\n" +
            "            <a href=\"#\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-edit'\" style=\"width:120px;margin-left: 250px\"" +
            " onclick=\"addColumn('wrap-ul"+groupId+"')\">添加新列</a>\n" +
            "            <a href=\"#\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-add'\" style=\"width:120px\" " +
            "onclick='addGroup()'>添加新组</a>\n" +
            "            <a href=\"#\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-remove'\" style=\"width:120px\" onclick=\"reGroup('wrap-grup"+groupId+"')\">remove</a>\n" +
            "        </div>\n" +
            "        <div  id='wrap-ul"+groupId+"'>" +
            "<div id=\"wrap-column"+ulId+"\">"+
            "            <input type=\"text\" name='propCode1' id='propCode"+ulId+"' class=\"easyui-combobox\" style=\"width:200px; margin-left: 50px\" data-options=\"panelHeight:'auto'\" />\n" +
            "            <input type=\"text\" name='comparator1'id='comparator"+ulId+"' class=\"easyui-combobox comparator\" style=\"width: 180px;margin-left: 100px\" data-options=\"panelHeight:'auto'\" prompt=\"比较符\"/>\n" +
            "            <div style=\"display: inline-block;margin-left: 50px\" id='metadatavalue"+ulId+"' >\n" +
            "            </div><a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;'data-options=\"iconCls:'icon-cancel'\" onclick=\"rmColumn('wrap-column"+ulId+"')\"></a>&nbsp;&nbsp;\n" +
            "            </div>\n" +
            "        </div>\n" +
            "    </div>"

        $(inner).appendTo("#wrap-grup");
        $.parser.parse($("#wrap-ul"+groupId).parent());
        var categoryCode = getFormData(document.getElementById("ff")).categoryCode;
        initModelSearchSelect1("propCode"+ulId+"","metadatavalue"+ulId+"",categoryCode);
        $("#comparator"+ulId).combobox({
            data:[
                {text:"=",value:"EQUAL"},
                {text:">",value:"GREAT"},
                {text:">=",value:"GREAT_EQUAL"},
                {text:"<",value:"LITTLE"},
                {text:"<=",value:"LITTLE_EQUAL"},
                {text:"<>",value:"NOT_EQUAL"},
                {text:"包含",value:"CONTAIN"}]
        });
    }
    function rmColumn(id) {
        $('#'+id).remove();
    }
    function reGroup(id) {
        $('#'+id).remove();
    }
    function spliceSql(id) {
        var add=0;
        var data = getFormData(document.getElementById("sql"));
        var criteria="";
        var subscript=0;//存下标
        for(var a=1;a<=groupId;a++){
            var ComplexQueryVO=[];
            var andOr="andOr"+a;
            var zss=$("#wrap-ul"+a).children().length//每个组的条件
            if(zss==0){
                continue;
            }
            // console.log(data[andOr]);
            if(  data[andOr] == undefined){
                var dh=0;
                if(zss==1){
                    alert("OR的条件必须多个")
                    return;
                }
                //当前组是or
                criteria=criteria+ "Criteria.orOperator(";
                ComplexQueryVO.andOr ="OR";
                for (var i=0;i<zss;i++){
                    var y=i+subscript;
                    var propCode=data.propCode1[y];
                    var comparator=data.comparator1[y];
                    var value=data.propValue1[y];
                    if(propCode != null &&propCode != ""
                        && comparator != null &&comparator != ""
                        &&value != null &&value != "" ){
                        if(dh==1){
                            criteria= criteria+","
                        }
                        if(Number(value)){
                            criteria= criteria+"Criteria.where(\"properties\").elemMatch(Criteria.where(\"code\").is(\""+propCode+"\").and(\"value\")."+comparator+"("+value+"))";
                        }else {
                            criteria= criteria+"Criteria.where(\"properties\").elemMatch(Criteria.where(\"code\").is(\""+propCode+"\").and(\"value\")."+comparator+"(\""+value+"\"))";
                        }
                    }
                    dh=1;
                }
                criteria=criteria+");";
            }else{
                //当前组是and
                if(add==1){
                    alert("add只允许存在一次");
                    return;
                }
                add=1;
                criteria=criteria+"Criteria"
                for (var i=0;i<zss;i++){
                    var y=i+subscript;
                    var propCode=data.propCode1[y];
                    var comparator=data.comparator1[y];
                    var value=data.propValue1[y];
                    if(propCode != null &&propCode != ""
                        && comparator != null &&comparator != ""
                        &&value != null &&value != "" ){
                        if(Number(value)){
                            criteria= criteria+".andOperator(Criteria.where(\"properties\").elemMatch(Criteria.where(\"code\").is(\""+propCode+"\").and(\"value\")."+comparator+"("+value+"))";
                        }else {
                            criteria= criteria+".andOperator(Criteria.where(\"properties\").elemMatch(Criteria.where(\"code\").is(\""+propCode+"\").and(\"value\")."+comparator+"(\""+value+"\"))";
                        }
                    }
                }
                criteria=criteria+"));";
            }
            subscript=subscript+zss;
        }
        criterias=criteria;
        $("#cq").window("close");
    }
    function updateColumn(id) {
        var ulId=upId+1;
        var inner = "<div id=\"update-column"+ulId+"\">\n"+
            "            <input type=\"text\" name='propCodeU'id='propCodeUpdate"+ulId+"' class=\"easyui-combobox\" style=\"width:200px; margin-left: 50px\" data-options=\"panelHeight:'auto'\" />\n" +
            "            <div style=\"display: inline-block;margin-left: 50px\" id='metadatavalueU"+ulId+"' >\n" +
            "            </div><a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;'data-options=\"iconCls:'icon-cancel'\" onclick=\"rmColumn('update-column"+ulId+"')\"></a>&nbsp;&nbsp;\n" +
            "            </div>" +
            "        </div>\n"

        $(inner).appendTo("#"+id);
        $.parser.parse($("#propCodeUpdate"+ulId).parent());
        var categoryCode = getFormData(document.getElementById("ff")).categoryCode;
        upId=upId+1;
        initModelSearchSelect1("propCodeUpdate"+ulId+"","metadatavalueU"+ulId+"",categoryCode,"updateValue");
    }
    /*批量修改字段*/
    function batchUpdate(page,size) {

        var dtd = $.Deferred();
        var data = getFormData(document.getElementById("ff"));
        setPropGroup(data);
        updateValue(data);
        var jsonobj = {};
        jsonobj = $.extend(true,jsonobj,transfer(data));
        jsonobj = JSON.stringify(jsonobj);
        console.log(jsonobj)

        $.ajax({
            url:config.url + "/model/batchUpdate",
            dataType:"json",
            type:'post',
            data:jsonobj,
            contentType:"application/json;charset=UTF-8",
            xhrFields:{withCredentials:true},
            beforeSend:function () {
                $.messager.progress({
                    title:"请稍等",
                    interval:50,
                    text:"操作进行中..."
                });
            },
            success:function (data) {
                if (data.code == "200") {
                    $.messager.progress("close");
                    $("#update").window("close");
                    $.messager.alert("提示","操作成功","info",function () {
                        searchBy(1,100);
                    });
                }else{
                    $.messager.progress("close");
                    $.messager.alert("提示",data.msg,"error");
                }

            },
            error:function (data) {

            }
        });
    }
    function updateValue(prom) {
        var data = getFormData(document.getElementById("batchUpdate"));
        var propertiesUpdate=[];
        if( !data  ){
            return;
        }
        if((typeof data.updateValue=='object')&& data.updateValue.constructor==Array){
            if(data.propCodeU.length == data.updateValue.length){
                for (var i=0;i<data.propCodeU.length;i++){
                    var pc=data.propCodeU[i];
                    var pv=data.updateValue[i];
                    var CategoryMetadataValue={code:pc,value:pv}
                    propertiesUpdate.push(CategoryMetadataValue);
                }
            }else {
                alert("前后不对等改不了喽！！！")
            }

        }else{
            var CategoryMetadataValue={code:data.propCodeU,value:data.updateValue}
            propertiesUpdate.push(CategoryMetadataValue);
        }
        prom.propertiesUpdate=propertiesUpdate;
        return prom;
    }
    function searchCategory(value){
        if (!value) {
            $.messager.alert("提示", "请输入型号码！",'info');
            return;
        }
        $.ajax({
            url:config.url+"/model/getModelByCode/"+value,
            type:"get",
            dataType:"json",
            xhrFields:{withCredentials:true},
            success:function (data) {
                if (data.msg) {
                    $.messager.alert("提示", data.msg,'info');
                }else {
                    $.messager.alert("提示", "未知品类",'info');
                }
            },
            error:function (data) {
                $.messager.alert("提示", "查询错误",'error');
            }
        });
    }
</script>
</html>
